<template>
    <div class="container">
        <header class="navbar">
            <div class="platform-name">智慧课程平台</div>
        </header>
        <div class="content">
            <aside class="sidebar">
                <div class="user-info">
                    <h2>个人信息</h2>
                    <p v-if="profile.username">你好，{{ profile.username }}</p>
                    <div v-if="profile">
                        <p><strong>ID:</strong> {{ profile.id }}</p>
                        <p><strong>Email:</strong> {{ profile.email }}</p>
                        <p><strong>Role:</strong> {{ profile.role }}</p>
                        <p><strong>Enroll Time:</strong> {{ profile.enroll_time }}</p>
                        <p><strong>Followers:</strong> {{ (profile.followers || []).length }}</p>
                        <p><strong>Following:</strong> {{ (profile.following || []).length }}</p>
                    </div>
                    <button @click="logout" class="logout-btn">登出</button>
                </div>
                <nav class="sidebar-nav">
                    <h3>导航</h3>
                    <ul>
                        <li><router-link to="/coursePlatformHomepage" class="nav-button">课程管理</router-link></li>
                        <li><router-link to="/favorites" class="nav-button">收藏夹</router-link></li>
                        <li><router-link to="/follow-list" class="nav-button">关注列表</router-link></li>
                    </ul>
                </nav>
            </aside>
            <main class="main-content">
                <section class="notifications">
                    <h2>通知信息</h2>
                    <p>目前暂无通知。</p>
                </section>
            </main>
        </div>
    </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      profile: {} // 保持初始化为空对象
    };
  },
  mounted() {
    const storedProfile = sessionStorage.getItem('userProfile');
    if (!storedProfile) {
      this.$router.push('/login');
    } else {
      this.profile = JSON.parse(storedProfile);
    }
  },
  methods: {
    async logout() {
      try {
        await axios.post('/backend/api/account/logout/', {}, {
          withCredentials: true,
        });
        sessionStorage.removeItem('userProfile');
        this.$router.push('/login');
      } catch (error) {
        console.error('Logout failed:', error);
      }
    },
  },
};
</script>

<style scoped>
.container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100%;
    max-width: 1600px;
    margin: 0 auto;
}

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 25px;
    background-color: #4a90e2;
    color: white;
}

.platform-name {
    font-size: 28px;
    font-weight: bold;
}

.content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: 300px;
    padding: 25px;
    background-color: #f8f9fa;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.user-info {
    margin-bottom: 30px;
    font-size: 16px;
}

.user-info h2 {
    font-size: 24px;
    margin-bottom: 15px;
}

.logout-btn {
    background-color: #4a90e2;
    color: white;
    border: none;
    padding: 12px 12px;
    border-radius: 900px;
    cursor: pointer;
    font-size: 17px;
    margin-top: 15px;
    width: 80%;
}

.nav-button {
    border: none;
    cursor: pointer;
    display: block;
    width: 50%;
    padding: 15px 15px;
    background-color: #4a90e2;
    color: white;
    text-decoration: none;
    font-size: 18px;
    border-radius: 900px;
    text-align: center;
    transition: background-color 0.3s;
}
.sidebar-nav ul {
    list-style-type: none;
    padding: 0;
    width: 100%;  /* 确保 ul 占满整个宽度 */
    display: flex;
    flex-direction: column;
    align-items: center;  /* 居中对齐子元素 */
}

.sidebar-nav li {
    margin-bottom: 15px;
    width: 100%;  /* 让 li 元素占满整个宽度 */
    display: flex;
    justify-content: center;  /* 水平居中其子元素 */
}


.nav-button:hover {
    background-color: #3a7bc0;
}

.main-content {
    flex: 1;
    padding: 25px;
    background-color: #ffffff;
    overflow-y: auto;
}

.notifications {
    background-color: #f5f5f5;
    padding: 25px;
    border-radius: 8px;
}

h2 {
    color: #333;
    margin-top: 0;
    font-size: 24px;
}

h3 {
    color: #333;
    margin-bottom: 15px;
    font-size: 22px;
}

p {
    font-size: 16px;
    line-height: 1.5;
}
</style>